
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:yp/config/index.dart';
import 'package:yp/helper.dart';
import 'package:yp/router.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import 'state.dart';

class UserBindLogic extends GetxController{
  final state = UserBindState();

  @override
  void onInit() {
    super.onInit();

    state.phoneTextController = TextEditingController();
    state.phoneFocusNode = FocusNode();

    state.passwordTextController = TextEditingController();
    state.passwordFocusNode = FocusNode();

    state.choosePrivacyAgreement = true;//false;
    state.loginBool = true;//false;

  }
  /* // 隐私协议 选择事件
  void choosePrivacyAgreement(bool chooseBool) {
    state.choosePrivacyAgreement = chooseBool;
    if (state.phoneTextController.text != '' && state.passwordTextController.text != '' && state.choosePrivacyAgreement) {
      state.loginBool = true;
    } else {
      state.loginBool = false;
    }
    update();
  } */
  // 输入框内容改变 事件
  void textControllerClick() {
    print('11111  textControllerClick');
    if (state.phoneTextController.text != '' && state.passwordTextController.text != '' && state.choosePrivacyAgreement) {
      state.loginBool = true;
    } else {
      state.loginBool = false;
    }
    update();
  }

  //请求发送验证码
  void sendSms(){
    var params = {
      "mobile":state.phoneTextController.text,
      "verify_code_type": "login_bind"
    };
    Api.sendSms(params).listen((event) {
      state.smsData = event;
      toast("验证码已发送,请注意查收");
      update();
    });

    state.timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (state.seconds == 0) {
        state.timer?.cancel();
        state.verifyStr = '重新发送';
        state.seconds = 60;
        update();
        return;
      }
      state.seconds = --state.seconds;
      state.verifyStr = "已发送${state.seconds}s";
      // print("已发送${state.seconds}s");
      update();
    });
  }

  /// 登录
  void onSubmit() {
    var thirdAccessToken = Get.parameters['thirdAccessToken'];
    var thirdContactNo = Get.parameters['thirdContactNo'];
    var thirdContactType = Get.parameters['thirdContactType'];
    var params = {
      "third_access_token":thirdAccessToken,
      "appid":Config.WECHAT_APPID,
      "third_contact_no":thirdContactNo,
      "third_contact_type":thirdContactType,
      "channel_num_id":99,
      "mobile": state.phoneTextController.text,
      "sms_verify_code": state.passwordTextController.text,
      "sms_verify_token": state.smsData["token"],
      "sms_verify_type": "login_bind",
      'use_platform':2
    };
    print('params-------$params');
    print('phone-------------${state.phoneTextController.text}');
    print('code----------------${state.passwordTextController.text}');
     Api.userBind(params).listen((event) {
      if (event["code"] != 0) {
        toast(event["message"]);
      } else {
        state.timer?.cancel();

        SpUtil.putString('empeName', event["usr_name"]);
        SpUtil.putInt('empeNumId', event["usr_num_id"]);
        SpUtil.putString('token', event["access_token"]);
        SpUtil.putString('number', state.phoneTextController.text);
        SpUtil.putInt(Config.IS_CHANGE_SHOP, 0);
        if(event["sign"]!=null && event["sign"]!='') {
          SpUtil.putString('usr_phone_num', event["usr_phone_num"]);
          SpUtil.putString('sign', event["sign"]);
          SpUtil.putInt('time_stamp', event["time_stamp"]);
        }
        SpUtil.remove(Config.GET_SHOP_INFO);
        toast("登录成功");
        Get.offAllNamed(RouteConfig.main);
      }
    });
  }


  @override
  void onClose() {
    super.onClose();
    state.timer?.cancel();
  }

}